﻿using CoreShop.App;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Model.FromBody;
using CoreShop.PrintService;
using CoreShop.Services;
using Newtonsoft.Json;
using SqlSugar;
using SqlSugar.Extensions;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using Yitter.IdGenerator;

namespace CoreShop.App
{
    public class SignalClientServices
    {

        /// <summary>
        /// 接收服务器发来的信息
        /// </summary>
        /// <param name="json"></param>
        public static bool ReceiveMessage(FromBodyModel frombody)
        {
            try
            {
                switch (frombody.code)
                {
                    case (int)GlobalEnumVars.MessageCode.Code101://101登录
                        return SetPosNotify(OnLogin(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code104://104转台(HT)
                        return SetPosNotify(DeskTransfer(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code107://107商品列表(CPLB)
                        return SetPosNotify(GetFoodList(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code111://111商品详情
                        return SetPosNotify(GetFoodDetail(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code112://112退菜(DPTC)
                        return SetPosNotify(FoodRefund(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code113://113点菜清单(DCQD)
                        return SetPosNotify(GetOrderingDetail(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code114://114整桌催菜
                        return SetPosNotify(UrgeAll(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code115://115单品催菜
                        return SetPosNotify(UrgeFood(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code116://116商品分类列表(CPFL)
                        return SetPosNotify(GetFoodCategoryList(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code117://117空闲餐台汇总(CTKX)
                        return SetPosNotify(GetDeskEmptyList(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code118://118餐台区域列表
                        return SetPosNotify(GetDeskCategoryList(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code128://128使用餐台汇总(YTKX)
                        return SetPosNotify(GetDeskInUseList(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code129://129常用备注列表(CYBZ)
                        return SetPosNotify(GetFoodTagList(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code130://130判断pos是否连接(ONLINE)
                        return SetPosNotify(Online(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code132://获取打印机列表
                        return SetPosNotify(GetPrinterList(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code134://打印预结账单
                        return SetPosNotify(SetPrintPrePayment(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code136://划菜
                        return SetPosNotify(CheckFoodSend(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code137://赠送
                        return SetPosNotify(PresentFood(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code141://消台
                        return SetPosNotify(DeskCancel(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code142://修改就餐人数
                        return SetPosNotify(SetEditPerson(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code143://获取参数设置
                        return SetPosNotify(GetSystemAppset(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code144://保存参数设置
                        return SetPosNotify(SetSystemAppset(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code145://打包
                        return SetPosNotify(SetFoodParcel(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code146://等叫
                        return SetPosNotify(SetFoodAwait(frombody));
                    case (int)GlobalEnumVars.MessageCode.Code148://当前班次
                        return SetPosNotify(GetOrderClass(frombody));
                    default: return false;
                }
            }
            catch { }
            return false;
        }

        /// <summary>
        /// 请求结果写到服务器
        /// </summary>
        /// <param name="entry"></param>
        public static bool SetPosNotify(FromBodyModel entry)
        {
            try
            {
                string postUrl = AppConstVars.systemsetup.corehubUrl + ":5046/Interface/PosNotify";
                string msg = JsonConvert.SerializeObject(entry);
                var result = HttpHelper.httpPost(postUrl, msg);
                return CommFunc.ConvertToBooten(result);
            }
            catch { }
            return true;
        }

        public static FromBodyModel GetFoodList(FromBodyModel entry)
        {
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var reqModel = JsonConvert.DeserializeObject<FoodReqModel>(data);

                var foodList = new SignalDataServices().GetFoodList(reqModel);
                entry.status = 0;
                entry.msg = "请求成功";
                entry.data = foodList;
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }


        /// <summary>
        /// 132	获取打印机列表
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel GetPrinterList(FromBodyModel entry)
        {
            try
            {
                var dataList = new SignalDataServices().GetPrinterList();
                entry.status = 0;
                entry.msg = "请求成功";
                entry.data = dataList;
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        public static FromBodyModel GetFoodDetail(FromBodyModel entry)
        {
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var reqModel = JsonConvert.DeserializeObject<DatabaseModel>(data);

                var specList = new SignalDataServices().GetFoodSpecList(reqModel.id);
                var taskListAll = new corecmsfoodtaskServices().QueryFromOrdering(0, reqModel.id, entry.storeId);
                var taskList = taskListAll.Select(p => new DatabaseModel { id = p.id, name = p.name }).ToList();
                var feedListAll = new corecmsfoodfeedServices().QueryFromOrdering(0, reqModel.id, entry.storeId);
                var feedList = feedListAll.Select(p => new FeedReqModel { id = p.id, name = p.name }).ToList();
                entry.status = 0;
                entry.msg = "请求成功";
                entry.data = new { specList, taskList, feedList };
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        public static FromBodyModel GetDeskEmptyList(FromBodyModel entry)
        {
            try
            {
                var deskList = new SignalDataServices().GetDeskEmptyList();
                entry.status = 0;
                entry.msg = "请求成功";
                entry.data = deskList;
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        public static FromBodyModel GetDeskCategoryList(FromBodyModel entry)
        {
            try
            {
                var deskList = new SignalDataServices().GetDeskCategoryList();
                entry.status = 0;
                entry.msg = "请求成功";
                entry.data = deskList;
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        public static FromBodyModel GetFoodCategoryList(FromBodyModel entry)
        {
            try
            {
                var resultList = new List<FoodTypeModel>();
                var typeList = new SignalDataServices().GetFoodCategoryList();

                if ((typeList != null) && (typeList.Count > 0))
                {
                   
                    var reqModel = new FoodReqModel() { foodTypeId = 0, name = "" };
                    var foodList = new SignalDataServices().GetFoodList(reqModel);

                    FoodTypeModel allNode = new FoodTypeModel();
                    allNode.foodList = foodList;
                    allNode.name = "全部";
                    allNode.id = 0;
                    resultList.Add(allNode);

                    foreach (var item in typeList)
                    {
                        var childs = foodList.Where(p => p.foodTypeId == item.id).ToList();
                        if ((childs != null) && (childs.Count > 0))
                        {
                            item.foodList = childs;
                            resultList.Add(item);
                        }
                    }

                }

                entry.status = 0;
                entry.msg = "请求成功";
                entry.data = resultList;
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        /// <summary>
        /// 113	点菜清单(DCQD)
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel GetOrderingDetail(FromBodyModel entry)
        {
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var reqModel = JsonConvert.DeserializeObject<OrderingReqModel>(data);
                var detail = new corecmsfoodorderingServices().QueryByOrderId(reqModel.orderId);
                var master = new corecmsfoodorderingmasterServices().QueryByDeskId(reqModel.deskId, reqModel.orderId);//开台模式

                if (master == null)
                {
                    entry.msg = "订单不存在";
                    entry.data = "";
                    entry.status = 1;
                    return entry;
                }

                var desk = new corecmsfooddeskServices().GetById(reqModel.deskId);

                var feedList = new corecmsfoodorderingfeedServices().QueryByOrderId(reqModel.orderId);
                var taskList = new corecmsfoodorderingtaskServices().QueryByOrderId(reqModel.orderId);

                foreach (var item in detail)
                {
                    item.feedList = feedList.Where(p => p.detailId.ObjToInt() == item.id).ToList();
                    item.taskList = taskList.Where(p => p.detailId.ObjToInt() == item.id).ToList(); ;
                }


                if (desk != null) desk.fullName = CommFunc.FormatDeskSpellNumber(desk.name, master.number);
                entry.status = 0;
                entry.msg = "请求成功";
                entry.data = new
                {
                    detail,
                    master,
                    desk
                };
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        public static FromBodyModel GetDeskInUseList(FromBodyModel entry)
        {
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var reqModel = JsonConvert.DeserializeObject<DeskTypeModel>(data);
                var deskList = new SignalDataServices().GetDeskInUseList(reqModel.categoryId, reqModel.type, reqModel.id);
                entry.status = 0;
                entry.msg = "请求成功";
                entry.data = deskList;
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        
        public static FromBodyModel GetFoodTagList(FromBodyModel entry)
        {
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var reqModel = JsonConvert.DeserializeObject<DataTypeModel>(data);
                var tagList = new corecmsfoodtagServices().QueryAsync(reqModel.type);
                entry.status = 0;
                entry.msg = "请求成功";
                entry.data = tagList;
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        public static FromBodyModel OnLogin(FromBodyModel entry)
        {
            entry.status = 1;
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var loginModel = JsonConvert.DeserializeObject<UserRequestModel>(data);
                loginModel.password = Security.Encrypt(loginModel.password);
                var userModel = new SignalDataServices().OnLogin(loginModel);
                if (userModel == null)
                {
                    entry.status = 1;
                    entry.msg = "登录失败";
                    entry.data = "";
                }
                else
                {
                    entry.status = 0;
                    entry.msg = "登录成功";
                    entry.data = userModel;
                }
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        /// <summary>
        /// 获取参数设置
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel GetSystemAppset(FromBodyModel entry)
        {
            entry.status = 1;
            try
            {
                var corecmssystemappset = new corecmssystemappsetService().GetById(entry.userId);
                if (corecmssystemappset == null)
                {
                    corecmssystemappset = new corecmssystemappset();
                    corecmssystemappset.sysUserId = entry.userId;
                }
                entry.status = 0;
                entry.msg = "获取成功";
                entry.data = corecmssystemappset;
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        /// <summary>
        /// 获取参数设置
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel GetOrderClass(FromBodyModel entry)
        {
            entry.status = 1;
            entry.msg = "当前系统未当班";
            try
            {
                //corecmsnumberdetail.classId存最后一个交班的ID
                var numberModel = new corecmsnumberdetailService().SelectDataById((int)GlobalEnumVars.SerialNumberType.交班单号);
                if ((numberModel != null) && (numberModel.classId > 0))
                {
                    var orderclass = new corecmsfoodorderclassServices().GetById(numberModel.classId);
                    if ((orderclass != null) && (!orderclass.classOff.ObjToBool()))
                    {
                        entry.status = 0;
                        entry.msg = "获取成功";
                        entry.data = orderclass;
                    }
                }
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        /// <summary>
        /// 获取参数设置
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel SetSystemAppset(FromBodyModel entry)
        {
            entry.status = 1;
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var model = JsonConvert.DeserializeObject<corecmssystemappset>(data);
                var appsetService = new corecmssystemappsetService();
                var corecmssystemappset = appsetService.GetById(model.sysUserId);
                if (corecmssystemappset == null)
                {
                    corecmssystemappset = new corecmssystemappset();
                    corecmssystemappset.sysUserId = model.sysUserId;

                }
                corecmssystemappset.orderingPrtId = model.orderingPrtId;
                corecmssystemappset.prePayPrtId = model.prePayPrtId;
                corecmssystemappset.settlementPrtId = model.settlementPrtId;
                var bl = appsetService.InsertOrUpdate(corecmssystemappset);

                if (bl)
                {
                    entry.status = 0;
                    entry.msg = "保存成功";
                }
                entry.data = corecmssystemappset;
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }
               
        
        public static FromBodyModel DeskCancel(FromBodyModel entry)
        {
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var model = JsonConvert.DeserializeObject<DeskOpenRequestModel>(data);

                var orderingDetails = new corecmsfoodorderingServices().QueryByOrderId(model.deskId);
                if ((orderingDetails != null) && (orderingDetails.Count > 0))
                {
                    entry.msg = "有已订货的数据，不能消台";
                    return entry;
                }
                var ordering = orderingDetails.FirstOrDefault();
                var bl = new corecmsfoodorderingmasterServices().DeleteById(ordering.orderId);
                if (!bl)
                {
                    entry.msg = "消台出错";
                    return entry;
                }
                entry.status = 0;
                entry.msg = "消台成功";
            }
            catch (Exception ex)
            {
                entry.status = 1;
                entry.msg = "开台失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        public static FromBodyModel SetEditPerson(FromBodyModel entry)
        {
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var model = JsonConvert.DeserializeObject<OrderPerson>(data);
                var orderingMaster = new corecmsfoodorderingmasterServices().QueryById(model.orderId);
                if (orderingMaster == null)
                {
                    entry.msg = "订单不存在";
                    return entry;
                }
                orderingMaster.person = model.person;
                var bl = new corecmsfoodorderingmasterServices().Update(orderingMaster);
                if (!bl)
                {
                    entry.msg = "消台出错";
                    return entry;
                }
                entry.status = 0;
                entry.msg = "消台成功";
            }
            catch (Exception ex)
            {
                entry.status = 1;
                entry.msg = "开台失败";
                entry.data = ex.Message;
            }
            return entry;
        }
             

        public static FromBodyModel DeskTransfer(FromBodyModel entry)
        {
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var model = JsonConvert.DeserializeObject<OrderingReqModel>(data);
                var orderingMaster = new corecmsfoodorderingmasterServices().GetById(model.orderId);
                entry.status = 1;
                entry.data = "";
                if (orderingMaster == null)
                {
                    entry.msg = "转台失败,请选择一张要转出的桌台订单";
                    return entry;
                }
                var deskModel = new corecmsfooddeskServices().GetById(model.deskId);
                if (orderingMaster == null)
                {
                    entry.msg = "转台失败,请选择一张要转入的桌台号";
                    return entry;
                }
                orderingMaster.deskId = model.deskId;
                var bl = new corecmsfoodorderingmasterServices().Update(orderingMaster);
                if (!bl)
                {
                    entry.msg = "转台失败,提交转台订单时失败";
                    return entry;
                }
                entry.status = 0;
                entry.msg = "转台成功.";
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        /// <summary>
        /// 114	整桌催菜(ZZCC)
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel UrgeFood(FromBodyModel entry)
        {
            try
            {
                string msg = "";
                var data = JsonConvert.SerializeObject(entry.data);
                var model = JsonConvert.DeserializeObject<OrderingReqModel>(data);
                var bl = SetPrintTask(model.id, 0, (int)GlobalEnumVars.PrintbillType.Urge, ref msg);
                if (!bl)
                {
                    entry.msg = msg;
                    return entry;
                }
                entry.status = 0;
                entry.msg = "操作成功.";
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        /// <summary>
        /// 划菜
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel CheckFoodSend(FromBodyModel entry)
        {
            try
            {
                string msg = "";
                var data = JsonConvert.SerializeObject(entry.data);
                var model = JsonConvert.DeserializeObject<OrderingReqModel>(data);

                var bl = new corecmsfoodorderingServices().Change<corecmsfoodordering>()
                    .Update(a => new corecmsfoodordering()
                    {
                        callType = (int)GlobalEnumVars.CallType.CheckSend
                    }, a => a.id == model.id);

                if (!bl)
                {
                    entry.msg = msg;
                    return entry;
                }
                entry.status = 0;
                entry.msg = "操作成功.";
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        
        /// <summary>
        /// 137赠送
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel PresentFood(FromBodyModel entry)
        {
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var model = JsonConvert.DeserializeObject<OrderingReqModel>(data);
                var orderingSelected = new corecmsfoodorderingServices().GetById(model.id);
                if (orderingSelected == null)
                {
                    entry.msg = "数据明细不存在。";
                    return entry;
                }
                if (orderingSelected.dataType == (int)GlobalEnumVars.OrderingDataType.Refund)
                {
                    entry.msg = "退送明细，不允许赠送。";
                    return entry;
                }
                orderingSelected.dataType = (int)GlobalEnumVars.OrderingDataType.Present;
                orderingSelected.sourcePrice = orderingSelected.realPrice;
                orderingSelected.realPrice = 0;
                orderingSelected.realAmount = orderingSelected.realPrice * orderingSelected.quantity;
                orderingSelected.allAmount = orderingSelected.realAmount + orderingSelected.feedAmount + orderingSelected.taskAmount;
                var bl = new corecmsfoodorderingServices().InsertOrUpdate(orderingSelected);
                if (!bl)
                {
                    entry.msg = "保存失败。";
                    return entry;
                }
                entry.status = 0;
                entry.msg = "操作成功.";
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        /// <summary>
        /// 115	整单催菜(DPCC)
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel UrgeAll(FromBodyModel entry)
        {
            try
            {
                string msg = "";
                var data = JsonConvert.SerializeObject(entry.data);
                var model = JsonConvert.DeserializeObject<OrderingReqModel>(data);
                var bl = SetPrintTask(0, model.orderId, (int)GlobalEnumVars.PrintbillType.Urge, ref msg);
                if (!bl)
                {
                    entry.msg = msg;
                    return entry;
                }
                entry.status = 0;
                entry.msg = "操作成功.";
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        /// <summary>
        /// 叫起，起菜，催菜等操作
        /// </summary>
        /// <param name="batchId"></param>
        /// <param name="type">type=1时，执行当前行</param>
        /// <returns></returns>
        public static bool SetPrintTask(long detailId, long orderId, int billtypeId, ref string msg)
        {
            long batchId = (long)YitIdHelper.NextId();
            var orderingServices = new corecmsfoodorderingServices();

            var where = Expressionable.Create<corecmsfoodordering>();
            if (detailId > 0)
                where = where.And(p => p.id == detailId);
            else
                where = where.And(p => p.orderId == orderId);
            List<corecmsfoodordering> orderingTasks = orderingServices.AsQueryable().Where(where.ToExpression()).ToList();
            if (orderingTasks.Count == 0)
            {
                msg = "没有可操作的数据。";
                return false;
            }
            var ordering = orderingTasks.FirstOrDefault();
            var orderingMaster = new corecmsfoodorderingmasterServices().GetById(ordering.orderId);
            var result = new corecmsprinttaskdetailServices().SavePrintTask(orderingTasks, orderingMaster, billtypeId, batchId);
            return result;
        }
               

        /// <summary>
        /// 112	单品退菜(DPTC)
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel FoodRefund(FromBodyModel entry)
        {
            try
            {
                var orderingService = new corecmsfoodorderingServices();
                var data = JsonConvert.SerializeObject(entry.data);
                var regModel = JsonConvert.DeserializeObject<FoodRefundReqModel>(data);
                string msg = "";
                var bl = orderingService.SetFoodRefund(regModel, ref msg);
                entry.msg = "退菜成功.";
                if (bl) entry.status = 0;
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        /// <summary>
        /// 检查是否在线，返回status=0证明客户端收到数据
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel Online(FromBodyModel entry)
        {
            try
            {
                entry.code = 0;
                entry.status = 0;
                entry.data = "";
                entry.msg = "请求成功";
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        /// <summary>
        /// 打印预结算单
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel SetPrintPrePayment(FromBodyModel entry)
        {
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var regModel = JsonConvert.DeserializeObject<OrderingReqModel>(data);
                var printManager = new SignalPrintManager();
                var printModel = printManager.GetPrintBillModel(regModel.orderId, entry.userId, (int)GlobalEnumVars.PrintbillType.PrePayment);
                string msg = "";
                entry.status = printManager.SetPrintPrePayment(printModel, ref msg);
                entry.data = "";
                entry.code = 0;
                entry.msg = msg;
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }


        /// <summary>
        /// 小程序订单打印
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool SetOrderPrint(OrderReqModel model)
        {
            bool result = false;
            var orderMasters = model.orderMasterList;
            var orderDetailList = model.orderDetailList;
            var schemedetails = new corecmsprintschemedetailServices().AsQueryable()
            .Where(p => (p.billtypeId == (int)GlobalEnumVars.PrintbillTypeTag.Account || p.billtypeId == (int)GlobalEnumVars.PrintbillType.takeOut) && p.selectedFlag == true).ToList();
            if ((schemedetails == null) || (schemedetails.Count == 0)) return result;

            var reportSrv = new ReportServices();
            var ordermasterSrv = new corecmsfoodordermasterServices();
            foreach (var orderMaster in orderMasters)
            {
                var schemedetail = orderMaster.sourceType == 2 ? schemedetails.Find(p => p.billtypeId == (int)GlobalEnumVars.PrintbillType.takeOut && p.selectedFlag) : schemedetails.Find(p => p.billtypeId == (int)GlobalEnumVars.PrintbillTypeTag.Account && p.selectedFlag);
                if (schemedetail == null) return result;
                var billtemplate = new corecmsprintbilltemplateServices().GetById(schemedetail.templateId);
                if (billtemplate == null) return result;

                //厨房打印
                var orderings = orderDetailList.Where(p => p.orderId == orderMaster.orderId).ToList();
                ordermasterSrv.SetOrderDetailPrint(orderMaster, orderings);
                //小票打印
                reportSrv.SetPrintSettlement(orderMaster, billtemplate, false);
            }
            return result;
        }

        
        /// <summary>
        /// 145打包
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel SetFoodParcel(FromBodyModel entry)
        {
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var model = JsonConvert.DeserializeObject<OrderingReqModel>(data);
                var where = Expressionable.Create<corecmsfoodordering>();
                if (model.id > 0)
                    where = where.And(p => p.id == model.id);
                else
                    where = where.And(p => p.orderId == model.orderId);
                var orderingServices = new corecmsfoodorderingServices();
                List<corecmsfoodordering> orderingList = orderingServices.AsQueryable().Where(where.ToExpression()).ToList();

                if ((orderingList == null) || (orderingList.Count == 0))
                {
                    entry.msg = "数据明细不存在。";
                    return entry;
                }
                foreach (var ordering in orderingList)
                {
                    ordering.parcel = "打包";
                }

                var bl = new corecmsfoodorderingServices().InsertOrUpdate(orderingList);
                if (!bl)
                {
                    entry.msg = "保存失败。";
                    return entry;
                }
                entry.status = 0;
                entry.msg = "操作成功.";
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }

        /// <summary>
        /// 146等叫
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static FromBodyModel SetFoodAwait(FromBodyModel entry)
        {
            try
            {
                var data = JsonConvert.SerializeObject(entry.data);
                var model = JsonConvert.DeserializeObject<OrderingReqModel>(data);
                var where = Expressionable.Create<corecmsfoodordering>();
                if (model.id > 0)
                    where = where.And(p => p.id == model.id);
                else
                    where = where.And(p => p.orderId == model.orderId);
                var orderingServices = new corecmsfoodorderingServices();
                List<corecmsfoodordering> orderingList = orderingServices.AsQueryable().Where(where.ToExpression()).ToList();

                if ((orderingList == null) || (orderingList.Count == 0))
                {
                    entry.msg = "数据明细不存在。";
                    return entry;
                }
                long orderId = 0;
                long batchId = (long)YitIdHelper.NextId();
                foreach (var ordering in orderingList)
                {
                    orderId = ordering.orderId;
                    if (ordering.callType == (int)GlobalEnumVars.CallType.Default)
                    {
                        ordering.callType = (int)GlobalEnumVars.CallType.Await;
                        ordering.batchId = batchId;
                    }
                }

                var bl = orderingServices.InsertOrUpdate(orderingList);
                if (!bl)
                {
                    entry.msg = "保存失败。";
                    return entry;
                }
                //打印叫起单
                orderingServices.SetOrderingToPrint(batchId, orderId, (int)GlobalEnumVars.PrintbillType.Await);

                entry.status = 0;
                entry.msg = "操作成功.";
            }
            catch (Exception ex)
            {
                entry.msg = "请求失败";
                entry.data = ex.Message;
            }
            return entry;
        }



        private static void DownLoadFoodList()
        {
            var exchange = new corecmsexchangeServices().GetById(AppConstVars.sysUser.storeId);
            string msg = "";
            if (exchange != null)
            {
                exchange.timeStampPos = new PosExchangeModel();
                exchange.timeStampRmo = new PosExchangeModel();

                var remoteService = new PosRemoteServices(AppConstVars.systemsetup.adminApiUrl);
                try
                {
                    exchange.status = true;
                    long storeId = AppConstVars.sysUser.storeId;
                    string token = AppConstVars.sysUser.ccessToken;
                }
                catch { }
            }
        }

    }
}
